package com.lablzh.gdxy.controller;

import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.BonusProject;
import com.lablzh.gdxy.entity.PushfreeProject;
import com.lablzh.gdxy.entity.Sciachievement;
import com.lablzh.gdxy.repository.BonusProjectRepository;
import com.lablzh.gdxy.service.BonusProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BonusController {

    @Autowired
    BonusProjectService bonusProjectService;
    @Autowired
    UserDetailsUtil userDetailsUtil;
    @Autowired
    BonusProjectRepository bonusProjectRepository;

//    private String stuCode;//学号
//    private String name;//姓名
//    private String major;//专业
//    private String classes;//班级
//    private String grade;//年级
//    private String type;//类型：包括集体公益，学生工作，文化体育，科技创新，个人荣誉，其他
//    private String projectName;//项目名称
//    private String miniType;//细分类型
//private String audit;//审核情况
//private String teacher;//指导教师

    /**
     * 多条件查询奖学金
     * stuCode;//学号
     * name;//姓名
     * major;//专业
     * classes;//班级
     * grade;//年级
     * type;//类型：包括集体公益，学生工作，文化体育，科技创新，个人荣誉，其他
     * projectName;//项目名称
     * miniType;//细分类型
     * audit;//审核情况
     * teacher;//指导教师
     *
     * @param bonusProject
     * @param page
     * @return
     */
    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/queryBonusProject")
    public Map getQueryAllBonusProject(@RequestBody(required = false) BonusProject bonusProject,
                                       @RequestParam(name = "page", defaultValue = "0") Integer page) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("stuCode", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher(" name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("projectName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("miniType", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("audit", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("teacher", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<BonusProject> example = Example.of(bonusProject, exampleMatcher);
        Pageable designPage = PageRequest.of(page - 1, 20, Sort.by(Sort.Direction.DESC, "id"));
        Page<BonusProject> all = bonusProjectRepository.findAll(example, designPage);
        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", all.getTotalPages());
        data.put("total", all.getTotalElements());
        data.put("bonus", all.getContent());
        return data;
    }

    //    查询所有bonus
    @PreAuthorize("hasAnyRole('instructor')")
    @GetMapping("/bonus/queryAll")
    public List<BonusProject> queryAllBonus() {
        return bonusProjectService.selectallbonusproject();
    }

    //查询某个学生的所有bonus
    @GetMapping("/bonus/queryBySid")
    public List<BonusProject> queryBySid(@RequestParam("stuCode") String stuCode) {
        String role = userDetailsUtil.getUserRole();
        String username = userDetailsUtil.getUserDetails();
        if (role.equals("ROLE_instructor")) {
            return bonusProjectService.selectstudentbonusproject(stuCode);
        } else {
            if (username.equals(stuCode)) {
                return bonusProjectService.selectstudentbonusproject(stuCode);
            } else {
                return null;
            }
        }
    }


    //    查询bonus
    @GetMapping("/bonus/query")
    public BonusProject queryBonusPoint(@RequestParam("id") Integer id) {
        return bonusProjectService.selectbonusproject(id);
    }

    //    删除bonus
    @PostMapping("/bonus/delete")
    public String deleteBonusPoint(@RequestParam("id") Integer id) {
        String role = userDetailsUtil.getUserRole();
        String username = userDetailsUtil.getUserDetails();
        if (role.equals("ROLE_instructor")) {
            return bonusProjectService.deletebonusproject(id);
        } else {
            if (username.equals(bonusProjectService.selectbonusproject(id).getStuCode())) {
                return bonusProjectService.deletebonusproject(id);
            } else {
                return "你无权删除！";
            }
        }

    }


    //  更新bonus
    @PostMapping("/bonus/update")
    public String updateBonusPoint(@RequestBody BonusProject bonusProject) {
        String role = userDetailsUtil.getUserRole();
        String username = userDetailsUtil.getUserDetails();
        if (role.equals("ROLE_student")) {
            if (!bonusProject.getStuCode().equals(username)) {
                return "你无权修改！";
            }

        }
        bonusProject.setAudit("未审");
        return bonusProjectService.addbonusproject(bonusProject);
    }

    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/bonus/audit")
    public String auditBonus(@RequestParam("id") Integer id,
                             @RequestParam("audit") String audit) {
        return bonusProjectService.auditbonusproject(id, audit);
    }

    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/bonus/selectbyaudit")
    public List<BonusProject> selectbyaudit(
            @RequestParam("audit") String audit) {
        return bonusProjectService.selectbonusProjectbyaudit(audit);
    }
    /**
     * 下载报表
     *
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @PreAuthorize("hasAnyRole('teacher','admin','instructor')")
    @PostMapping("/bonus/downloadExcel")
    public void downloadExcel(@RequestBody BonusProject bonusProject,
                              @RequestParam("sort") String sort,
                              HttpServletRequest request,
                              HttpServletResponse response) throws FileNotFoundException {
        bonusProjectService.downloadExcel(bonusProject, request, sort, response);
    }
}
